package com.ywq.app.web.mvc.misc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.ArtActivityBiz;
import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.domain.ArtActivity;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Encode;
@Controller
public class ArtActivityMiscController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private ArtActivityBiz artActivityBiz;
	/**
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/artActivity.htm")
	public String artRoom(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
			return "misc/artActivity/artActivity";
			}
	/**
	 * 活动展示列表链接
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/artActivityGrid.htm")
	public String artPicGrid(Integer page, Integer pagesize,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModelUser = artActivityBiz.getArtActivityListForMisc(page,pagesize);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModelUser));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return null;
	}
	/**
	 * 添加活动页面
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/addArtActivityWeb.htm")
	public String addArtPicWeb(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		return "misc/artActivity/addArtActivityWeb";
	}
	/**
	 * 添加活动
	 * @param request
	 * @param response
	 * @param artPic
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/insertArtActivity.htm", method = RequestMethod.POST)
	public String insertArtActivity(HttpServletRequest request,
			HttpServletResponse response, ArtActivity artActivity) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artActivityBiz.insertArtActivity(artActivity);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("ArtActivity failure.", e);
			}
		}
		return null;
	}
	/**
	 * 活动详情
	 * @param roomId
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/showActivityDetail.htm")
	public String showActivityDetail(String activityId, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
			request.setAttribute("activityId", activityId);
		return "misc/artActivity/showActivityDetail";
	}
	/**
	 * 显示活动
	 * @param request
	 * @param response
	 * @param artRoom
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/displayActivity.htm", method = RequestMethod.POST)
	public String displayActivity(HttpServletRequest request,HttpServletResponse response, ArtActivity artActivity) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artActivity.setIsShow(true);
			artActivityBiz.updateArtActivity(artActivity);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("artActivity failure.", e);
			}
		}
		return null;
	}
	
	/**
	 * 关闭活动
	 * @param request
	 * @param response
	 * @param artRoom
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/misc/artActivity/hideActivity.htm", method = RequestMethod.POST)
	public String hideActivity(HttpServletRequest request,HttpServletResponse response, ArtActivity artActivity) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artActivity.setIsShow(false);
			artActivityBiz.updateArtActivity(artActivity);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("hideRoom failure.", e);
			}
		}
		return null;
	}
	//更新活动页面
	@RequestMapping(value = "/misc/artActivity/updateActivityDetail.htm")
	public String updateActivityDetail(String activityId, HttpServletRequest request, HttpServletResponse response) throws IOException {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try {
				ArtActivity artActivity = artActivityBiz.getArtActivityById(activityId);
				request.setAttribute("artActivity", artActivity);
				request.setAttribute("activityId", activityId);
			} catch (AppBusinessException e) {
				pw = response.getWriter();
				pw.print("error");
				if (logger.isErrorEnabled()) {
					logger.error("updateActivityDetail failure.", e);
				}
			}
		
		return "misc/artActivity/updateActivityDetail";
	}
	//更新活动
	@RequestMapping(value = "/misc/artActivity/updateArtActivity.htm", method = RequestMethod.POST)
	public String updateArtActivity(HttpServletRequest request,HttpServletResponse response, ArtActivity artActivity) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artActivityBiz.updateArtActivity(artActivity);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("updateArtActivity failure.", e);
			}
		}
		return null;
	}

	//删除
	@RequestMapping(value = "/misc/artActivity/deleteActivity.htm", method = RequestMethod.POST)
	public String deleteActivity(String identifier,HttpServletRequest request,HttpServletResponse response, ArtRoom artRoom) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			artActivityBiz.deleteActivity(identifier);
			pw = response.getWriter();
			pw.print("success");
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("hideRoom failure.", e);
			}
		}
		return null;
	}
	@RequestMapping(value = "/misc/artActivity/artPersonList.htm")
	public String artPersonList(String activityId,Integer page, Integer pagesize,HttpServletRequest request,
			HttpServletResponse response) throws IOException {
	
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			if (page == null) {
				page = 1;
			}
			PageModel pageModelUser = artActivityBiz.artPersonList(page,pagesize,activityId);
			pw = response.getWriter();
			pw.print(JSONObject.fromObject(pageModelUser));
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return null;
	}
	
}
